import win32com.client
import random
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=data.mdb;'
conn.Open(DSN)
rs1 = win32com.client.Dispatch(r'ADODB.Recordset')
rs_name = 'T_KIND'
rs1.Open('[' + rs_name + ']', conn, 1, 3)
rs2 = win32com.client.Dispatch(r'ADODB.Recordset')
rs_name = 'T_SHI'
rs2.Open('[' + rs_name + ']', conn, 1, 3)
rs2.MoveFirst()
j=0
ls=[]
while not rs2.EOF:
    for i in range(rs2.Fields.Count):
        if rs2.Fields(i).Name=='D_SHI':
            ls.append(rs2.Fields(i).Value)
    if j>354: # number of src poems
        break
    rs2.MoveNext()
    j+=1
verse=[]
for line in ls:
    for vs in line.split(u"\uff0c"):
        for svs in vs.split(u"\u3002"):
            for ssvs in svs.split(u"\uff1f"):
                if ssvs.strip()!=u'':
                    verse.append(ssvs.strip())
vsDic={}
vsDicCount={}
for i in range(len(verse)):
    vs=verse[i]
    for ch in range(len(vs)-1):
        if vs[ch] in vsDic.keys():
            flag=False
            for j in range(len(vsDic[vs[ch]])):
                if vs[ch+1]==vsDic[vs[ch]][j]:
                    vsDicCount[vs[ch]][j]+=1
                    flag=True
                    break
            if flag==False:
                vsDic[vs[ch]].append(vs[ch+1])
                vsDicCount[vs[ch]].append(1)
        else:
            vsDic[vs[ch]]=[vs[ch+1]]
            vsDicCount[vs[ch]]=[1]
    if i!=len(verse)-1:
        if vs[-1] in vsDic.keys():
             if not verse[i+1][0] in vsDic[vs[-1]]:
                 vsDic[vs[-1]].append(verse[i+1][0])
                 vsDicCount[vs[-1]].append(1)
        else:
            vsDic[vs[-1]]=[verse[i+1][0]]
            vsDicCount[vs[-1]]=[1]        
##for c in vsDic.keys():
##    str=c+":"
##    for i in range(len(vsDic[c])):
##        str+="%s%d"%(vsDic[c][i],vsDicCount[c][i])
##    print str
def makePeom(c=None,Length=5,LineNum=8):
    if c==None:
        tmp=vsDic.keys()
        c=tmp[random.randint(0,len(tmp)-1)]
    elif not c in vsDic.keys():
        return False
    count=0
    verse=[]
    while True:
        verse.append(c)
        count+=1
        if (not c in vsDic.keys()) or (count>=Length*LineNum):
            break
        maxCount=max(vsDicCount[c])
        maxVal=0
        for i in range(len(vsDic[c])):
            tmp=vsDicCount[c][i]*2+random.randint(0,maxCount) #random choose next word
            if vsDic[c][i]==c:
                tmp-=vsDicCount[c][i] # reduce repeat
            if tmp>maxVal and ((count<2) or (vsDic[c][i]!=verse[count-2])):
                if(vsDic[c][i]!=u'\uff1b'): # more filter
                    maxVal=tmp
                    choice=i
        c=vsDic[c][choice]
    count=0
    for c in verse:
        print c,
        count+=1
        if count%(Length*2)==0:
            print u'\u3002'
        elif count%Length==0:
            print u'\uff0c',
#makePoem("*".decode('gb2312'),7,8) # * is a chinese charactor


        
    
    
    

